package com.GamersCom.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.GamersCom.db.User;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.UserMgr;
import com.GamersCom.util.GlobalCONST;


/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = (HttpSession) request.getSession();
		String nextView = "registration.jsp";
		RequestDispatcher rd = request.getRequestDispatcher(nextView);
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String nextView = "registration.jsp";
		// get the model factory
		MgrFactory model = (MgrFactory) this.getServletContext().getAttribute(
				"mgrFactory");
		UserMgr userMgr = model.getUserMgr();

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String email = request.getParameter("email");
		String securityQuestion = request.getParameter("securityquestion");
		String answer = request.getParameter("answer");
		int accessLevel = GlobalCONST.USER; // user access level
		String status = GlobalCONST.PENDING;

		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setFirstName(firstname);
		user.setLastName(lastname);
		user.setEmail(email);
		user.setAccessLevel(accessLevel);
		user.setStatus(status);
		user.setSecurityQuestion(securityQuestion);
		user.setAnswer(answer);

		String uuid = UUID.randomUUID().toString(); // generate the token for
													// activation
		user.setToken(uuid);

		//send back user input
		request.setAttribute("firstname", firstname);
		request.setAttribute("lastname", lastname);
		request.setAttribute("username", username);
		request.setAttribute("email", email);
		request.setAttribute("securityquestion", securityQuestion);
		request.setAttribute("answer", answer);
		
		// Validate if user exist 
		if ( userMgr.getUserByUsername(username) != null )
		{
			request.setAttribute("usernameError", "The username already exist!");
		} 
		else if ( userMgr.getUserByEmail(email) != null) // validate if email exist
		{
			request.setAttribute("emailError", "The email address already exist!");
		}
		else 
		{			
			try
			{
				//insert user
				userMgr.insertUser(user);
				
				request.setAttribute("msg", "You have successfully registered. Please wait for administrator's approval.");
				request.removeAttribute("firstname");
				request.removeAttribute("lastname");
				request.removeAttribute("username");
				request.removeAttribute("email");				
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (ClassNotFoundException e)
			{
				e.printStackTrace();
			}
		}
				
		request.getRequestDispatcher(nextView).forward(request, response);

	}

}
